<template>
	<view class="record-detail page-content">
		<nav-bar back right-text="保存"  @clickRight="back">
			{{ pageTitle }}
		</nav-bar>
		<!-- <component :is="componentName"></component> -->
		<view class="page-main">
			<component class="record-detail-main main" ref="componentRef" :is="currentComponent"></component>
		</view>
		
	</view>
</template>

<script setup>
	import { reactive, ref,shallowRef } from 'vue';
	import { onLoad } from '@dcloudio/uni-app';
	import {recordList} from '@/utils/config.js'
	import Breastfeeding from './../components/Breastfeeding';
	import MixedFeeding from './../components/MixedFeeding';
	import FormulaMilk from './../components/FormulaMilk';
	import NutritionalSupplements from './../components/NutritionalSupplements';
	import WaterIntake from './../components/WaterIntake';
	import Pumping from './../components/Pumping';
	import DiaperChange from './../components/DiaperChange';
	import Pooping from './../components/Pooping';
	import Urination from './../components/Urination';
	import Sleep from './../components/Sleep';
	import SolidFood from './../components/SolidFood';
	import Bathing from './../components/Bathing';
	import Height from './../components/Height';
	import Weight from './../components/Weight';
	import HeadCircumference from './../components/HeadCircumference';
	import BodyTemperature from './../components/BodyTemperature';
	import Discomfort from './../components/Discomfort';
	import Medication from './../components/Medication';
	import Vaccination from './../components/Vaccination';
	import Others from './../components/Others';
	import dayjs from 'dayjs'
	import {setHistoryStore,
		updateHistoryStore,
		updateStore,
		setStore,
		setHistoryList,
	} from '@/utils/index.js'
	
	// 定义一个响应式变量存储页面参数
	const queryParams = ref({});
	
	const pageTitle= ref('')
	const componentRef= ref(null)
	const currentComponent = ref('')
	
	
	const back=()=>{
		console.log(componentRef.value)
		componentRef.value.submit().then(res=>{
			const {id,type} = queryParams.value
			console.log('添加的数据',res);
			res.updateTime = dayjs().valueOf()
			
			let setHistoryKey = ['NutritionalSupplements','Medication','SolidFood']
			
			if(setHistoryKey.includes(type)){
				setHistoryList(type,res)
			}
			
			if(id){
				updateStore(type,id,res)
				uni.showToast({
					title: `修改成功`
				})
				
			}else{
				setStore(type,res)
				if(type === 'Pooping' && res.isHnb){
					setStore('DiaperChange',{
						time:res.time,
						updateTime:dayjs().valueOf(),
						remark:'拉臭臭换尿布'
					})
				}else if(type === 'Urination' && res.isHnb){
					setStore('DiaperChange',{
						time:res.time,
						updateTime:dayjs().valueOf(),
						remark:'嘘嘘换尿布'
					})
				}
				uni.showToast({
					title: `添加成功`
				})
				
			}
			
			uni.switchTab({
				url: '/pages/tabbar/tabbar-1/tabbar-1'
			});
			
		})
	}
	
	
	// 使用 onLoad 钩子函数
	onLoad((options) => {
		
	  console.log('页面加载时的参数:', options,options.type,recordList.map(item=>item.key));
	  queryParams.value = options; // 将参数赋值给响应式变量
	  switch (options.type) {
	    case 'Breastfeeding':
	      currentComponent.value = Breastfeeding;
	      break;
	    case 'MixedFeeding':
	      currentComponent.value = MixedFeeding;
	      break;
	    case 'FormulaMilk':
	      currentComponent.value = FormulaMilk;
	      break;
	    case 'NutritionalSupplements':
	      currentComponent.value = NutritionalSupplements;
	      break;
	    case 'WaterIntake':
	      currentComponent.value = WaterIntake;
	      break;
	    case 'Pumping':
	      currentComponent.value = Pumping;
	      break;
	    case 'DiaperChange':
	      currentComponent.value = DiaperChange;
	      break;
	    case 'Pooping':
	      currentComponent.value = Pooping;
	      break;
		  
	    case 'Urination':
	      currentComponent.value = Urination;
	      break;
	    case 'Sleep':
	      currentComponent.value = Sleep;
	      break;
	    case 'SolidFood':
	      currentComponent.value = SolidFood;
	      break;
	    case 'Bathing':
	      currentComponent.value = Bathing;
	      break;
	    case 'Height':
	      currentComponent.value = Height;
	      break;
	    case 'Weight':
	      currentComponent.value = Weight;
	      break;
	    case 'HeadCircumference':
	      currentComponent.value = HeadCircumference;
	      break;
	    case 'BodyTemperature':
	      currentComponent.value = BodyTemperature;
	      break;
	    case 'Discomfort':
	      currentComponent.value = Discomfort;
	      break;
	    case 'Medication':
	      currentComponent.value = Medication;
	      break;
	    case 'Vaccination':
	      currentComponent.value = Vaccination;
	      break;
	    case 'Others':
	      currentComponent.value = Others;
	      break;
	    default:
	      break;
	  }
	  
	  let name = recordList.find(item=>item.key === options.type).name
	  pageTitle.value=name
	});
	
	// // 动态加载组件
	// async function loadComponent(name) {
	//   try {
	//     const component = await import(`./../components/${name}.vue`);
	// 	console.log(component,'componentvalue');
	//     currentComponent.value = component.default || component;
	// 	console.log(currentComponent.value,'currentComponent.value');
	//   } catch (error) {
	//     console.error(`Failed to load component ${name}:`, error);
	//     currentComponent.value = null;
	//   }
	// }
</script>

<style lang="scss">
.record-detail{
	.record-detail-main{
		padding: 20rpx;
	}
}
</style>
